package com.lhf.azkaban.springbatch.example.batchwriter;

import com.lhf.azkaban.springbatch.example.utils.DateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.batch.item.file.FlatFileFooterCallback;
import org.springframework.batch.item.file.FlatFileHeaderCallback;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.batch.api.chunk.listener.ItemWriteListener;
import java.io.IOException;
import java.io.Writer;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:95780
 * @Date: 13:18 2019/11/21
 * @Description: Flat文件格式写入类  实现了分隔符格式
 */
@Service
public class RepayOrderFlatFileItemWriter<RepayOrderInfo> extends FlatFileItemWriter implements ItemWriteListener {

    private static final Logger logger = LogManager.getLogger(RepayOrderFlatFileItemWriter.class);
    @Value("${repay.orders.bigpath}")
    private String bigpath;
    @Value("${repay.orders.bigname}")
    private String bigname;
    @Value("${repay.orders.outpath}")
    private String outpath;
    @Value("${repay.orders.outname}")
    private String outname;

    public RepayOrderFlatFileItemWriter() {
        logger.info("======RepayOrderFlatFileItemWriter Begin ======   time:   " + DateUtil.parseDateTime(LocalDateTime.now(), DateUtil.PATTERN_DEFAULT_DATA));
        repayOrderOfDelimitedLine();
    }

    private void repayOrderOfDelimitedLine() { /*分隔符文件读取配置*/
        ClassPathResource classPathResource = new ClassPathResource(outpath + outname);
        setResource(classPathResource);
        setAppendAllowed(true);
        setLineAggregator(new DelimitedLineAggregator<RepayOrderInfo>() {{
            setDelimiter("|");
            setFieldExtractor(new BeanWrapperFieldExtractor<RepayOrderInfo>() {{
                setNames(new String[]{"userId", "productId", "contractNo", "amount", "repayType", "repayMethod"});
            }});
        }});

        setHeaderCallback(new FlatFileHeaderCallback() { /*写文件头*/
            @Override
            public void writeHeader(Writer writer) throws IOException {
                writer.write("#begin time: " + DateUtil.parseDateTime(LocalDateTime.now(), DateUtil.PATTERN_DEFAULT_DATA));
            }
        });

        setFooterCallback(new FlatFileFooterCallback() { /*写文件尾*/
            @Override
            public void writeFooter(Writer writer) throws IOException {
                writer.write("#end time: " + DateUtil.parseDateTime(LocalDateTime.now(), DateUtil.PATTERN_DEFAULT_DATA));
            }
        });
    }

    @Override
    public void beforeWrite(List<Object> list) throws Exception {
        logger.info("======ItemWriteListener beforeWrite======");
    }

    @Override
    public void afterWrite(List<Object> list) throws Exception {
        logger.info("======ItemWriteListener afterWrite======");
        logger.info(list);
    }

    @Override
    public void onWriteError(List<Object> list, Exception e) throws Exception {
        logger.info("======ItemWriteListener onWriteError======");
    }
}
